package com.qianchang.microfinance.view.mine.loan;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.FormatUtil;
import com.dm.library.utils.ToastUtil;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.MineUserInfo;
import com.qianchang.microfinance.bean.WithdrawalStatusBean;
import com.qianchang.microfinance.global.DMApplication;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.MainActivity;
import com.qianchang.microfinance.view.base.BaseActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 贷款状态显示界面
 *
 *
 */
public class HSLoanWithdrawActivity extends BaseActivity {


    @BindView(R.id.lv_schedule_id)
    ListView scheduleListView;
    @BindView(R.id.withdraw_money_id)
    TextView withdrawMoney;
    @BindView(R.id.tv_bank_name_id)
    TextView bankName;
    @BindView(R.id.tv_bank_nub_id)
    TextView bankText;
    @BindView(R.id.btn_finish)
    Button finishBut;
    @BindView(R.id.tv_progress_id)
    TextView progText;

    private HSWithdrawAdapter adapter;
    private String userId;
    private String bidId;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hswithdraw_layout);
        ButterKnife.bind(this);
        initView();
        initData();

        getFindWithdStatus();

    }

    @Override
    protected void initView() {
        super.initView();
        setTitleName(getString(R.string.withdraw_hs));

    }

    @Override
    protected void initData() {
        super.initData();

        Bundle bundle = getBundle();
        if(bundle != null){
            userId = bundle.getString("userId");
            bidId = bundle.getString("bidId");
        }

        adapter = new HSWithdrawAdapter(HSLoanWithdrawActivity.this);
        scheduleListView.setAdapter(adapter);


    }

    @OnClick({R.id.btn_finish})
    public void onViewClicked(View view){
        switch (view.getId()){
            case R.id.btn_finish:
//                startActivity(HSLoanWithdrawActivity.class, true);
                startActivity(MainActivity.class, true);
                break;
            default:
                break;
        }
    }


    //获取贷款提现状态
    //返回说明：status：0（提交）1（1次提现）2（冷静期）3（完成）
    private void getFindWithdStatus(){

        HttpParams params = new HttpParams();
        params.put("bidId", bidId);
        params.put("userId",userId);

        DMOKHttpUtils.getInstance().post(getApplicationContext(), DMConstant.ApiURL.FIND_WITHD, params, new RequestCallBack<WithdrawalStatusBean>() {
            @Override
            public void onSuccess(JsonResult<WithdrawalStatusBean> response) {
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    WithdrawalStatusBean withdrawalStatusBean = response.getObject();
                    refreshUi(withdrawalStatusBean);
                }else{
                    dismiss();
                    ToastUtil.getInstant().show(getApplicationContext().getApplicationContext(), response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(getApplicationContext().getApplicationContext(), msg);
            }

            @Override
            public void onConnectFailure(String msg) {

            }
        });
    }

    private void refreshUi(WithdrawalStatusBean withdrawalStatusBean){
        if(withdrawalStatusBean == null) return;
        String amount = withdrawalStatusBean.getAmount();
        String cardStr = withdrawalStatusBean.getCard();
        String status = withdrawalStatusBean.getStatus();
        if(status != null && !status.equals("")){
            int mstatus = Integer.valueOf(status);
            adapter.setIndex(mstatus);
        }
        withdrawMoney.setText("¥:"+amount);
        int miamount = Integer.valueOf(amount);
        double tvaduit = (miamount * 0.18);
        double tvActualArri = (miamount - tvaduit);
//        progText.setText("处理中" + "(" + "扣除快速信审费" + FormatUtil.numKbPointFormat(tvaduit) + "元," + "实际到账" + FormatUtil.numKbPointFormat(tvActualArri) + "元)");                           //快速审核费
        progText.setText("确认扣除居间服务费" + FormatUtil.numKbPointFormat(tvaduit) + "元");                           //快速审核费
        bankName.setText(cardStr);
    }

}
